<?php

class SQLQuery
{
   private $_query;

   function __constructor()
   {
      $this->_query = "";
   }
   
   public function insert($into, $insert, $value)
   {

      $insertString="";

      if ($insert=="")
      {
         $insertString = $insert;
      }
      else
      {
         foreach($insert as $field)
         {
            if (next($insert))
            {
               $insertString=$insertString."`$field`,";
            }
            else
            {
               $insertString=$insertString."`$field`";
            }
         }
      }
      
      $this->_query = $this->_query."INSERT INTO `$into` ($insertString)";

      $valueString="";

      foreach($value as $val)
      {
         if (next($value))
         {
            if(substr($val, 0, 12)=="DES_ENCRYPT(")
            {
               $valueString=$valueString."$val,";
            }
            else
            {
               $valueString=$valueString."'$val',";
            }
         }
         else
         {
            if(substr($val, 0, 12)=="DES_ENCRYPT(")
            {
               $valueString=$valueString."$val";
            }
            else
            {
               $valueString=$valueString."'$val'";
            }
         }
      }
      
      $this->_query = $this->_query." VALUES ($valueString)";
      
   }
   
   public function selectConjunction($from, $select, $where=null, $limit=null)
   {
      $selectString="";

      if ($select=="*")
      {
         $selectString = $select;
      }
      else
      {
         foreach($select as $field)
         {
            if (next($select))
            {
               $selectString=$selectString."`$field`,";
            }
            else
            {
               $selectString=$selectString."`$field`";
            }
         }
      }
      
      $this->_query = $this->_query."SELECT $selectString FROM `$from`";

      if (isset($where))
      {
         $whereString = "";
         foreach($where as $field=>$value)
         {
            if(next($where))
            {
               $whereString = $whereString."$field='$value' AND ";
            }
            else
            {
               $whereString = $whereString."$field='$value'";
            }
         }
         $this->_query = $this->_query." WHERE $whereString";
      }
   
      if (isset($limit))
      {
         $this->_query = $this->_query." LIMIT $limit";
      }
   }

   public function request()
   {
      return $this->_query;
   }

   public static function decrypt($field, $key)
   {
      return "DES_DECRYPT($field,'$key')";
   }

   public static function encrypt($value, $key)
   {
      return "DES_ENCRYPT('$value','$key')";
   }

}

//$thanks = new WWS_Query();

//$thanks-> insert("Users", array("ID","Password"),  array("BUB",WWS_Query::encrypt("BUBBLE","RUBY")));

//echo $thanks->request();

?>